package cn.jji8.PlayerLimitQuickShop;

import cn.jji8.PlayerLimitQuickShop.Data.Shop;
import cn.jji8.PlayerLimitQuickShop.logger.Logger;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 数据类，用于保存玩家购买数据
 * 方便插件获取玩家的数据
 * */
public class Datas {
    HashMap<String, Shop> shopMap = new HashMap<>();//用于存储已经被加载的shop
    File workFile;
    /**
     * 给定一个工作路径，构造一个Datas
     * */
    public Datas(File workfile){
        this.workFile=workfile;
    }
    /**
     * 删除某个商店的限制
     * */
    public void delete(String location){
        shopMap.remove(location);
        File file = new File(workFile,location);
        file.delete();
    }
    /**
     * 查询玩家是否可以向某个商店出售物品
     * */
    public boolean ifPlayerCanSellForShop(String location, Player player,double money){
        Logger.putdebug(player+"查询玩家是否可以出售");
        Shop shop = shopMap.get(location);
        if(shop==null){
            File file = new File(workFile,location);
            if(!file.exists()){
                Logger.putdebug(player+"没有商店"+location+"的配置文件，可以出售");
                return true;
            }
            shop = new Shop(file);
            shopMap.put(location,shop);
        }
        boolean isCan = shop.ifPlayerContinue(player,money);
        if(isCan){
            Logger.putdebug(player+"没有达到限制，可以出售");
            return true;
        }
        Logger.putdebug("玩家超出限制，不能出售");
        return false;
    }
    /**
     * 保存全部的数据
     * */
    public void saveAll(){
        for(Iterator<Shop> allShop = shopMap.values().iterator();allShop.hasNext();allShop.next().save());
    }
    /**
     * 设置某个商店玩家在指定时间内最多出售获得的钱数 单位：毫秒
     * */
    public void setShopTiemPlayerSellMaxMoney(String shopUuid,long time,double money){
        Logger.putdebug("设置"+shopUuid+"商店，每个玩家在"+time+"时间类，最大收购"+money+"原");
        Shop shop = shopMap.get(shopUuid);
        if(shop==null){
            File file = new File(workFile,shopUuid);
            shop = new Shop(file);
            shopMap.put(shopUuid,shop);
        }
        shop.setPlayerMaxMoneyForTime(time,money);
    }
    /**
     * 设置某个商店全部玩家最多出售物品可获得的总钱数 单位：毫秒
     * */
    public void setShopAllSellMaxMoney(String shopUuid,long time,double money){
        Shop shop = shopMap.get(shopUuid);
        if(shop==null){
            File file = new File(workFile,shopUuid);
            shop = new Shop(file);
            shopMap.put(shopUuid,shop);
        }
        shop.setAllMaxMoneyForTime(time,money);
    }
}
